import EventEmit from './Event';

class Operation {
	public operationMap: Map<string, any>;
	public currentOperation: any;
	constructor() {
		this.operationMap = new Map();
		this.currentOperation = null;
	}

	public getOperationWithId(id: string) {
		if (this.operationMap.has(id)) {
			return this.operationMap.get(id);
		} else {
			return null;
		}
	}

	public setOperationWithId(id: string, value: any) {
		if (id && value) {
			this.operationMap.set(id, value);
			this.currentOperation = value;
			EventEmit.setInviteEventListener({ type: 'operation', event: 'widget' }, { data: value, code: 200, message: 'update operation success' });
		}
	}

	public removeOperationWithId(id: string) {
		if (this.operationMap.has(id)) {
			this.operationMap.delete(id);
		}
	}

	public clearOperation() {
		this.operationMap.clear();
	}

	/** 通知其他坐席, 当前坐席正在进行的操作 */
	public notifyOtherAgentsOperation(operationType: string, data: any, messageType: string, agentMembers: any[], clientId: string, callBack: (content: any, client: string) => void) {
		agentMembers.forEach(item => {
			let content = {
				operationType,
				data,
				messageType,
				type: 'broadcast-operation'
			};
			if (item.clientId !== clientId) {
				callBack(content, item.clientId);
			}
		});
	}
}

export default Operation;
